import time
import multiprocessing as mp
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.remote.webelement import WebElement

def webdriver_init():
    chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_argument("--proxy-server=http://218.75.158.153:3128")  # 代理ip
    chrome_options.add_argument('--headless')   # 增加无界面选项
    chrome_options.add_argument('log-level=3')
    chrome_options.add_argument('--disable-gpu')  # 如果不加这个选项，有时定位会出现问题
    chrome_options.add_argument('--window-size=1920,1080')   # 设置窗口大小, 窗口大小会有影响
    u=webdriver.Chrome(options=chrome_options,executable_path="c:\chromedriver.exe")
    return u
  
def play(url,i):
    u=webdriver_init()
    time.sleep(2)
    u.get(url)
    time.sleep(5)
    u.switch_to.frame(u.find_element_by_xpath('/html/body/table/tbody/tr[2]/td/iframe'))
    u.find_element_by_xpath('//*[@id="frame_user_score"]/div[2]/div/a').click()
    time.sleep(3)
    u.find_elements_by_class_name('course_courseware_folder')[0].click()
    time.sleep(2)
    try:
        u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_1"]/div/a').click()
    except:
        u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_2"]/div/a').click()
    u.find_elements_by_class_name('childSection')[i].click()
    time.sleep(80)
    u.quit()
    quit()
    
if __name__=='__main__':
    un=''
    pwd=''
    u=webdriver_init()
    u.get('http://edu.wuxuejiaoyu.cn/hnyyjsxy')
    u.find_element_by_xpath('//*[@id="firstPart"]/div[2]/div/div[2]').click()
    u.find_element_by_xpath('//*[@id="userName"]').send_keys(un)
    u.find_element_by_xpath('//*[@id="userPwd"]').send_keys(pwd)
    u.find_element_by_xpath('//*[@id="submitBtn"]').click()
    time.sleep(1)
    u.find_element_by_xpath('//*[@id="iul"]/li[2]/a/span').click()
    time.sleep(1)
    u.switch_to.frame(u.find_element_by_xpath('//*[@id="myFrame1"]'))
    sel=u.find_element_by_xpath('//*[@id="termSelect"]')
    
    urllist=[]
    for cell in Select(sel).options:
        cell.click()
        table = u.find_element_by_xpath('//*[@id="theTable"]')
        table_rows=table.find_elements_by_tag_name('tr')
        for i in range(1,len(table_rows)):
            if table_rows[i].find_elements_by_tag_name('td')[8].text=='开始学习':
                urllist.append(table_rows[i].find_elements_by_tag_name('td')[8].find_elements_by_tag_name('span')[0].get_attribute('ahref'))
        time.sleep(1)
    u.quit()
    
    for url in urllist:
        try:
            u=webdriver_init()
            time.sleep(2)
            u.get(url)
            time.sleep(5)
            u.switch_to.frame(u.find_element_by_xpath('/html/body/table/tbody/tr[2]/td/iframe'))
            u.find_element_by_xpath('//*[@id="frame_user_score"]/div[2]/div/a').click()
            time.sleep(3)
            u.find_elements_by_class_name('course_courseware_folder')[0].click()
            time.sleep(2)
            try:
                u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_1"]/div/a').click()
            except:
                u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_2"]/div/a').click()
            n=len(u.find_elements_by_class_name('childSection'))
            u.quit()
        except:
            time.sleep(5)
            u=webdriver_init()
            time.sleep(2)
            u.get(url)
            time.sleep(5)
            u.switch_to.frame(u.find_element_by_xpath('/html/body/table/tbody/tr[2]/td/iframe'))
            u.find_element_by_xpath('//*[@id="frame_user_score"]/div[2]/div/a').click()
            time.sleep(3)
            u.find_elements_by_class_name('course_courseware_folder')[0].click()
            time.sleep(2)
            try:
                u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_1"]/div/a').click()
            except:
                u.find_element_by_xpath('//*[@id="_DataListTD_tblDataList_2"]/div/a').click()
            n=len(u.find_elements_by_class_name('childSection'))
            u.quit()
        for i in range(n):
            mp.Process(target=play,args=(url,i)).start()
        time.sleep(120)